package com.basic.hellorabbit.controller;

import com.basic.hellorabbit.util.VerificationCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* @Description:    图形验证码
* @Author:         Joe
* @CreateDate:     2020/5/8 14:28
*/
@Controller
@RequestMapping(value = "/codes")
@Slf4j
public class ImageController {

    @RequestMapping(value = "/getcode",method = RequestMethod.GET,name = "获取图形验证码")
    public void getCode(HttpServletRequest request, HttpServletResponse response) throws Exception {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //声明图片类型为RGB
        BufferedImage bufferedImage = new BufferedImage(120,40,BufferedImage.TYPE_INT_RGB);
        //引入画笔
        Graphics graphics = bufferedImage.getGraphics();
        //设置画布背景颜色
        Color color = new Color(240,240,140);
        graphics.setColor(color);
        //填充画布
        graphics.fillRect(0,0,120,40);
        //获取验证码字符串
        String numeral = VerificationCode.getNumeral();
        log.info("numeral:{}",numeral);
        char[] chars = numeral.toCharArray();
        Random r = new Random();
        //JAVA中的Font有三种属性决定(1.字体名：(familyName),2.风格：(style),3.大小：(size))
        Font axesFont = new Font("宋体", Font.BOLD,32);
        for (int i = 0; i < chars.length; i++) {
            graphics.setFont(axesFont);
            //设置文本字符的颜色:setColor(color)
            graphics.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
            //设置文本字符的位置:drawString(String str , int x , int y),x与y属于str的左下角的坐标
            graphics.drawString(chars[i]+"",i*20,20+r.nextInt(20));
            //[干扰线]设置线条的位置:drawLine(int x1 , int y1， int x2 , int y2),x1与y1属于线条起点坐标，x2与y2属于线条终点坐标
            graphics.drawLine(r.nextInt(120), r.nextInt(40), r.nextInt(120), r.nextInt(40));
        }
        //放入session，进行验证
        request.getSession().setAttribute("code",numeral);
        //输入图片
        ImageIO.write(bufferedImage,"JPG",response.getOutputStream());

    }
    @RequestMapping(value = "/ycode",method = RequestMethod.POST,name = "验证图形验证码")
    public String yCode(Model model,String code,HttpServletRequest request) {
        String result="";
        if (code==null||code.equals("")){
            result="输入的验证码为空";
        }
        log.info("输入的code:{}",code);
        String codes = request.getSession().getAttribute("code").toString();
        log.info("session拿到的codes:{}",codes);
        if (code.equals(codes)){
            result="验证成功！";
        }else if (!code.equals(codes)&&!code.equals("")){
            result="验证失败,请输入正确的验证码！";
        }
        model.addAttribute("message",result);
        return "image";
    }
    @RequestMapping(value = "/index",method = RequestMethod.GET,name = "跳到验证页面")
    public String index(){
        return "image";
    }
}
